package cn.meizhu.org.controller;

import cn.meizhu.org.domain.Shop;
import cn.meizhu.org.query.ShopQuery;
import cn.meizhu.org.service.IEmployeeService;
import cn.meizhu.org.service.IShopService;
import cn.meizhu.util.AjaxResult;
import cn.meizhu.util.QueryResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/shop")
@RestController
@Api(tags = "员工维护接口")
//@CrossOrigin
public class ShopController {

    @Autowired
    private IShopService shopService;

    @PutMapping
    @ApiOperation(value = "部门添加或者修改", notes = "有id就是修改，没有id就是新增")
    public AjaxResult addAndUpdDepartment(@RequestBody Shop shop){
        try {
            if (shop.getId() == null){
                shopService.save(shop);
            }else {
                shopService.update(shop);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }


    @DeleteMapping("/{id}")
    @ApiOperation(value = "这是部门删除")
    public AjaxResult delDepartmentById(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询部门中指定数据")
    public Shop findDepartmentById(@PathVariable("id") Long id){
        return shopService.findById(id);
    }

    @GetMapping
    @ApiOperation(value = "查询部门全部数据")
    public List<Shop> findDepartments(){
        return shopService.findAll();
    }


    @PostMapping
    public QueryResult<Shop> queryPage(@RequestBody ShopQuery shopQuery){
        return shopService.queryPage(shopQuery);
    }

    /**
     * 批量删除
     * @return
     */
    @PatchMapping
    public AjaxResult patchDelDepartmentByIds(@RequestBody Long[] ids){
        try {
            shopService.patchShopByIds(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.me().setMessage(e.getMessage());
        }
    }

    @PostMapping("/settlement")
    private AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }


    @GetMapping("/through/{id}")
    public AjaxResult through(@PathVariable("id") Long id){
        System.out.println(id);
        try {
            shopService.updateBystate(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }
    @GetMapping("/overrule/{id}")
    public AjaxResult overrule(@PathVariable("id") Long id){
        try {
            shopService.overrule(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }
}
